package com.example.tuter.util;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeUnitUtil {


    public static String getTime(String time) {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8:00"));
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String DATE = sdf.format(Long.parseLong(time));
        return DATE.substring(DATE.indexOf("/") + 1);
    }


    public static String getTimeByTimestampString(Long time) {
        if (time == null) return null;
        StringBuilder str = new StringBuilder();

        try {

            Date date = new Date();
            long timeLag = date.getTime() - time;
            //如果时间间隔在一天以内
            if(timeLag<Constant.DAY_M){
                if(timeLag < Constant.MINUTE_M){
                    //几秒前
                    str.append(timeLag/Constant.SECOND_M);
                    str.append("秒前");
                }else if(timeLag <Constant.HOUR_M){
                    //几分钟前
                    str.append(timeLag/Constant.MINUTE_M);
                    str.append("分钟前");
                }else if(timeLag<Constant.DAY_M){
                    str.append(timeLag/Constant.HOUR_M);
                    str.append("小时前");
                }
                return str.toString();
            }

            Calendar instance = Calendar.getInstance();

            instance.setTime(new Date(time));
            if (instance.get(Calendar.YEAR) != Constant.YEAR) {
                str.append(instance.get(Calendar.YEAR));
                str.append("年");
            }
            str.append(instance.get(Calendar.MONTH));
            str.append("月");
            str.append(instance.get(Calendar.DAY_OF_MONTH));
            str.append("日");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return str.toString();
    }

    /**
     * 获取这周在全年的第几周
     * @param instance
     * @return
     */
    public static Integer getWeekInYear(Calendar instance) {
        return instance.get(Calendar.WEEK_OF_YEAR);
    }
}
